package com.foreknow.controller;

import com.foreknow.model.Gusetbook;
import com.foreknow.service.GuestbookeService;
import com.foreknow.service.impl.GusetbookeServiceInpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "ManagerServlet")
public class ManagerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);//但是这里必须调用doGet方法,因为提交默认的方法是doPOST,不调用会连接不到系统

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //因为从URL连接上获取信息要用doGet,而表单默认的提交方式是POST,所以要在doPOST里面调用doGet方法;
        //这里面的q就是LoginServlet里的请求转发中的q
        String q = request.getParameter("q");
        System.out.println(request.getParameter("name"));
        //判断这个q的名字等于那个方法的名字就自动调用那个方法
        if ("delete".equals(q)){
            delete(request,response);
        }else if("update".equals(q)){
            update(request,response);
        }else if("getById".equals(q)){
           getById(request,response);
        }else {
            list(request,response);
        }

    }

    protected  void list(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        //调用业务层查询所有留言信息的方法
        GuestbookeService service = new GusetbookeServiceInpl();
        List<Object> list = service.quertInfo();
        //将查询到的list集合保存到服务器
        request.setAttribute("list",list);
        //就可以将这个请求转发给list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);


    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        GusetbookeServiceInpl inpl = new GusetbookeServiceInpl();
       Boolean isRight=inpl.deleteBean(Integer.parseInt(req.getParameter("id")));
       if (isRight){
           req.getRequestDispatcher("/manager?q=list").forward(req,resp);
       }else {
                out.println("default");
       }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        GusetbookeServiceInpl inpl = new GusetbookeServiceInpl();
        Gusetbook gusetbook = new Gusetbook();
        gusetbook.setName(req.getParameter("name"));
        gusetbook.setPhone(req.getParameter("phone"));
        gusetbook.setEmail(req.getParameter("email"));
        gusetbook.setTitle(req.getParameter("title"));
        gusetbook.setContent(req.getParameter("content"));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        gusetbook.setCreatedtime(sdf.format(new Date()));
        int i = inpl.updateUser(Integer.parseInt(req.getParameter("user")), gusetbook);
        if (i>0){
            req.getRequestDispatcher("/manager?q=list").forward(req,resp);
        }else {
            out.println("default");
        }

    }
    protected void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询一条信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //调用service层根据id查询的方法
        GusetbookeServiceInpl inpl = new GusetbookeServiceInpl();
        Gusetbook gusetbook = inpl.getById(Integer.parseInt(req.getParameter("id")));
        if (gusetbook!=null){
            //将信息保存到服务器
            req.setAttribute("user",gusetbook);
            //请求转发
            req.getRequestDispatcher("/userinfo.jsp").forward(req,resp);

        }else {
            out.println("default");
        }



    }
}
